package com.fulihui.wgj;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.fulihui.starter.kafka.annotation.EnableKafkaProducer;
import com.fulihui.wgj.shutdown.event.ShutdownSignalEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;

/**
 * @author wang_wx
 * @date 2018-06-12
 */
@SpringBootApplication
@EnableDubboConfiguration
@EnableKafkaProducer
public class ServiceApplication {

    private static Logger LOGGER = LoggerFactory.getLogger(ServiceApplication.class);

    public static void main(String[] args) {
        // 非web项目启动方式
        ApplicationContext applicationContext = new SpringApplicationBuilder(ServiceApplication.class).web(false).run(args);
        shutdownHook(applicationContext);
    }

    /**
     * 注册JVM关闭钩子，捕捉Ctrl+C / kill pid 信号
     */
    private static void shutdownHook(ApplicationContext applicationContext) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            LOGGER.info("-> service application shutdown start.");
            //广播告诉所有需要处理的监听器，系统要停机啦~
            applicationContext.publishEvent(new ShutdownSignalEvent(new Object()));
        }));
    }
}
